package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectDanger;
import com.mxpio.erp.common.plm.entity.ProjectDeliverableTemplate;
import com.mxpio.erp.common.plm.entity.ProjectInfo;
import com.mxpio.erp.common.plm.entity.ProjectInfoTemplate;
import com.mxpio.erp.common.plm.service.ProjectInfoTemplateService;
import com.mxpio.erp.common.plm.service.ProjectTaskTemplateService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectInfoTemplateController", description = "项目信息模板")
@RestController
@RequestMapping("/erp/plm/pro/info/template/")
public class ProjectInfoTemplateController {

    @Autowired
    private ProjectInfoTemplateService projectInfoTemplateService;
    @Autowired
    private ProjectTaskTemplateService  projectTaskTemplateService;

    @GetMapping("list")
    @Operation(summary = "项目列表模板", description = "项目列表模板（无分页）", method = "GET")
    public Result<List<ProjectInfoTemplate>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectInfoTemplate> list = projectInfoTemplateService.list(ProjectInfoTemplate.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "项目列表模板", description = "项目列表模板（分页）", method = "GET")
    public Result<Page<ProjectInfoTemplate>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectInfoTemplate> pageResult = projectInfoTemplateService.listPage(ProjectInfoTemplate.class, page, criteria);
        return Result.OK(pageResult);
    }


    @GetMapping("list/{templateCode}")
    @Operation(summary = "根据模板编码获取模板列表", description = "根据模板编码获取模板列表", method = "GET")
    public Result<ProjectInfoTemplate> getById(@PathVariable(name = "templateCode", required = true) String templateCode) {
        ProjectInfoTemplate projectInfoTemplate = projectInfoTemplateService.getById(ProjectInfoTemplate.class, templateCode);
        return Result.OK(projectInfoTemplate);
    }

    @RequestMapping("add/{proCode}")
    @Operation(summary = "新增模板信息", description = "新增模板信息")
    public Result<?> add(@PathVariable(name = "proCode", required = true) String proCode) {
        return projectInfoTemplateService.saveTemplate(proCode);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑模板信息", description = "编辑模板信息", method = "PUT")
    public Result<ProjectInfoTemplate> edit(@RequestBody ProjectInfoTemplate projectInfoTemplate) {
        //更新任务中对应的项目信息
        projectTaskTemplateService.changeTaskTemplate(projectInfoTemplate);
        projectInfoTemplateService.update(projectInfoTemplate,new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context){
                Object o = context.getEntity();
                if (o instanceof ProjectDeliverableTemplate){
                    ProjectInfoTemplate parent = context.getParent();
                    ((ProjectDeliverableTemplate)o).setTemplateCode(parent.getTemplateCode());
                    ((ProjectDeliverableTemplate) o).setProCode(parent.getProCode());
                }
                return true;
            }
        });
        return Result.OK(projectInfoTemplate);
    }

    @DeleteMapping(value = "delete/{templateCode}")
    @Operation(summary = "删除危险信息",description = "删除危险信息", method = "DELETE")
    public Result<?> delete(@PathVariable(name = "templateCode") String templateCode) {
        projectInfoTemplateService.deleteAll(templateCode);
        return Result.OK("删除成功");
    }

}
